package com.goldplusgold.tradedelay.app.my.account;

import android.app.Activity;
import android.content.Context;

import com.example.account.manager.ParmasKey;
import com.goldplusgold.network.lib.exception.ServerException;
import com.example.account.manager.AppCacheSharedPreferences;
import com.goldplusgold.support.lib.utils.DeviceNetworkUtil;
import com.goldplusgold.support.lib.widget.AsyncTask;
import com.goldplusgold.tradedelay.app.my.networkservice.MyService;

/**
 * Created by Administrator on 2017/7/19.
 */

public class AccountNet {

    private Context context ;
    private AccountCallBack accountInterface ;



    public AccountNet(Context context , AccountCallBack accountInterface){
        this.context = context ;
        this.accountInterface = accountInterface ;
    }


    public void login_net(String mUserName , String mPassword){
        String imei = DeviceNetworkUtil.getIMEIWrap(context);
        new LoginAsyncTask(imei ,mUserName , mPassword).execute(AsyncTask.TYPE_NET);
    }



    public class LoginAsyncTask extends AsyncTask<Void, Void, UserInfo> {
        private String imei;
        private String mUserName ;
        private String mPassword ;

        public LoginAsyncTask(String imei , String mUserName , String mPassword) {
            this.imei = imei;
            this.mUserName = mUserName ;
            this.mPassword = mPassword ;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //showLoadingControl();
        }

        @Override
        protected UserInfo doInBackground(Void... params) {
            UserInfo userInfo = null;
            try {
                userInfo = MyService.getInstance().userLogin("android", mUserName, mPassword, "android", imei);
            } catch (final ServerException e) {
                e.printStackTrace();
                ((Activity)context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        accountInterface.fail(e);
                    }
                });

                //sendNetworkErrorMessage(NetWorkConstant.NETWORK_SERVER_ERROR_CODE, e.getMessage());
            } catch (final Exception e) {
                e.printStackTrace();
                ((Activity)context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        accountInterface.fail(e);
                    }
                });
                //sendNetworkErrorMessage(NetWorkConstant.NETWORK_ERROR_CODE, null);
            }
            return userInfo;
        }

        @Override
        protected void onPostExecute(UserInfo userInfo) {
            super.onPostExecute(userInfo);
            //dismisLoadingControl();
            if (userInfo != null) {
                String token = userInfo.getToken();
                String time = String.valueOf(System.currentTimeMillis());
                accountInterface.success(userInfo);


                AppCacheSharedPreferences.putCacheBoolean(ParmasKey.login_state , true);
                AppCacheSharedPreferences.putCacheString(ParmasKey.token , token);
                AppCacheSharedPreferences.putCacheString(ParmasKey.user_name ,userInfo.getUserName() );

                /*AppCacheSharedPreferences.putCacheString(ParmasKey.user_password , mPassword);

                String versionName = AppInfoUtils.getVersionName(getActivity());
                AppCollectedInfoUtil.initAppCollectedInfo(getActivity(), token, time, versionName);
                if (getActivity() == null && getActivity().isFinishing()) {
                    return;
                }
                getActivity().setResult(Activity.RESULT_OK);
                if (getActivity() instanceof UserActivity) {
                    ((UserActivity) getActivity()).operationAfterLogin();
                }*/
            }else{
                AppCacheSharedPreferences.getCacheBoolean(ParmasKey.login_state , false);
                /*UserInfoMananger.setIsLogin(false);*/
                accountInterface.fail(new Exception("未获得用户信息"));
            }
        }
    }


}
